package com.lagou.edu.factory;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Service;
import com.lagou.edu.utils.TransactionManager;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Service("proxyFactory")
public class ProxyFactory {

    @Autowired
    @Qualifier("transactionManager")
    private TransactionManager transactionManager;

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }


    public Object getJdkProxy(Object obj){

        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object result = null;
                try {
                    transactionManager.beginTransaction();

                    result = method.invoke(obj,args);

                    transactionManager.commit();
                }catch (Exception e){
                    e.printStackTrace();

                    transactionManager.rollback();

                    throw e;
                }
                return result;
            }
        });
    }



    public Object getCglibProxy(Object obj){
        return Enhancer.create(obj.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                Object result = null;
                try {
                    transactionManager.beginTransaction();

                    result = method.invoke(obj,objects);

                    transactionManager.commit();
                }catch (Exception e){
                    e.printStackTrace();

                    transactionManager.rollback();

                    throw e;
                }
                return result;
            }
        });
    }
}
